package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.macro.mall.dto.e.SysSpiderEntDTO;
import com.macro.mall.dto.o.SysSpiderOutDTO;
import com.macro.mall.mapper.BasicPlatformMapper;
import com.macro.mall.mapper.BasicSpsTemplateMapper;
import com.macro.mall.mapper.SpsPullTaskMapper;
import com.macro.mall.mapper.SpsPullTaskTemplateMidMapper;
import com.macro.mall.model.BasicPlatform;
import com.macro.mall.model.BasicSpsTemplate;
import com.macro.mall.model.BasicSpsTemplateExample;
import com.macro.mall.model.SpsPullTask;
import com.macro.mall.service.SysSpiderService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysSpiderServiceImpl implements SysSpiderService {
    @Autowired
    private SpsPullTaskMapper spsPullTaskMapper;
    @Autowired
    private BasicSpsTemplateMapper basicSpsTemplateMapper;//爬虫模板
    @Autowired
    private BasicPlatformMapper basicPlatformMapper;


    @Override
    public PageInfo<SysSpiderOutDTO> list(SysSpiderEntDTO sysSpiderEntDTO) {
        PageHelper.startPage(sysSpiderEntDTO.getPage(),sysSpiderEntDTO.getPageSize());
        //List<SpsPullTask> list = spsPullTaskMapper.list(sysSpiderEntDTO.getTemplateName());
        List<BasicSpsTemplate> list = basicSpsTemplateMapper.list(sysSpiderEntDTO.getName());

        PageInfo<BasicSpsTemplate> spsPullTaskPageInfo = new PageInfo<>(list);
        List<SysSpiderOutDTO> sysSpiderOutDTOs=new ArrayList();
        for (BasicSpsTemplate spsPullTask:list){
            SysSpiderOutDTO sysSpiderOutDTO = new SysSpiderOutDTO();
            sysSpiderOutDTO.setId(spsPullTask.getId());
            sysSpiderOutDTO.setName(spsPullTask.getName());
            sysSpiderOutDTO.setCreatedDt(spsPullTask.getCreatedDt());
            sysSpiderOutDTO.setUrl(spsPullTask.getPlatName());
            sysSpiderOutDTO.setStatus(spsPullTask.getStatus());
            sysSpiderOutDTO.setPlatId(spsPullTask.getBasicPlatformId());
            sysSpiderOutDTOs.add(sysSpiderOutDTO);
        }
        PageInfo<SysSpiderOutDTO> sysSpiderOutDTOPageInfo = new PageInfo<>(sysSpiderOutDTOs);
        sysSpiderOutDTOPageInfo.setTotal(spsPullTaskPageInfo.getTotal());
        return sysSpiderOutDTOPageInfo;
    }

    @Override
    public int setStatus(SysSpiderEntDTO sysSpiderEntDTO) {
        BasicSpsTemplate basicSpsTemplate = new BasicSpsTemplate();
        basicSpsTemplate.setId(sysSpiderEntDTO.getId());
        basicSpsTemplate.setStatus(sysSpiderEntDTO.getStatus());
        return basicSpsTemplateMapper.updateByPrimaryKey(basicSpsTemplate);
    }

    @Override
    public int modify(SysSpiderEntDTO sysSpiderEntDTO) {
        BasicSpsTemplate basicSpsTemplate = new BasicSpsTemplate();
        basicSpsTemplate.setId(sysSpiderEntDTO.getId());
        basicSpsTemplate.setBasicPlatformId(sysSpiderEntDTO.getPlatId());
        basicSpsTemplate.setName(sysSpiderEntDTO.getName());
        return basicSpsTemplateMapper.updateByPrimaryKey(basicSpsTemplate);
    }
}
